package com.situ.object.service.impl;


import com.situ.object.common.Page;
import com.situ.object.dao.CourseDao;
import com.situ.object.dao.impl.CourseDaoImpl;
import com.situ.object.entity.Course;
import com.situ.object.service.CourseService;

import java.util.ArrayList;
import java.util.List;

public class CourseServiceImpl implements CourseService {
    private CourseDao courseDao = new CourseDaoImpl();


    @Override
    public Page getForPage(Integer page, Integer limit, Course course) {
        Page pageInfo = new Page();

        //将sql作为参数传递给dao
        //where 1=1    用于解决条件为空的问题
        String sql = "select s.*,d.studentName as student_name from course s " + " left join student d on s.studentId=d.studentId  where s.status = 0 "; //需要修改
        //多个条件 动态拼接 where
        //参数
        List<Object> params = new ArrayList<>();

        System.out.println("sss："+course.getStudentId());
        if (course.getCourseName()!=null&& !"".equals(course.getCourseName())){
            sql += " and s.courseName like ? ";    //需要修改
            params.add("%"+ course.getCourseName() + "%");
        }
        if (course.getStudentId()!=null){
            sql += " and s.studentId = ? ";     //需要修改
            params.add(course.getStudentId());
        }



        Long count = courseDao.selectCount(sql,params);
        List list = courseDao.selectList(page,limit,sql,params);

        //将字段总数和
        pageInfo.setCount(count);
        pageInfo.setList(list);
        System.out.println("course:"+pageInfo);
        return pageInfo;
    }

    @Override
    public int add(Course course) {
        return courseDao.add(course);
    }

    @Override
    public int edit(Course course) throws Exception {
        return courseDao.update(course);
    }

    @Override
    public int delete(Integer id) {
        return courseDao.delete(id);
    }
}
